package cn.golxen.rsms.controller;

import cn.golxen.rsms.bean.bo.AddOrOutGoodsNumBo;
import cn.golxen.rsms.bean.bo.DelGoodsStoreBo;
import cn.golxen.rsms.bean.bo.GetGoodsLogByPeriodBo;
import cn.golxen.rsms.bean.bo.ImportExcelBo;
import cn.golxen.rsms.bean.entity.GoodsLog;
import cn.golxen.rsms.bean.entity.GoodsMain;
import cn.golxen.rsms.bean.entity.GoodsPurchase;
import cn.golxen.rsms.bean.entity.GoodsStore;
import cn.golxen.rsms.bean.vo.ResGoodsInfoVo;
import cn.golxen.rsms.bean.vo.ResGoodsSalesAnalysisVo;
import cn.golxen.rsms.commons.Result;
import cn.golxen.rsms.commons.annotation.AddLog;
import cn.golxen.rsms.mapper.GoodsMapper;
import cn.golxen.rsms.redis.GoodsRedis;
import cn.golxen.rsms.service.GoodsService;
import cn.golxen.rsms.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private LogService logService;
    @Autowired
    GoodsMain goodsMain;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    @Qualifier(value = "taskExecutor")
    private ThreadPoolTaskExecutor poolTaskExecutor;



    @GetMapping(value = "/get/barcodeBase")
    public Result getBarcodeInfo(@RequestParam String code){
        goodsMain=goodsService.getBarcodeInfo(code);
        return Result.successResult(goodsMain);
    }

    @GetMapping(value = "/get/goodsInfoByCode/{store}/{code}")
    public Result getGoodsInfoByCode(@PathVariable int store,@PathVariable String code){
//        long t1=System.currentTimeMillis();
        ResGoodsInfoVo resGoodsInfoVo=goodsService.getGoodsInfo(code,store);
//        log.info(String.valueOf(System.currentTimeMillis()-t1));
        return Result.successResult(resGoodsInfoVo);
    }

    @GetMapping(value = "/get/goodsInfoByCode2/{store}/{code}")
    public Result getGoodsInfoByCode2(@PathVariable int store,@PathVariable String code){
//        long t1=System.currentTimeMillis();
        ResGoodsInfoVo resGoodsInfoVo=goodsService.getGoodsInfo2(code,store);
//        log.info(String.valueOf(System.currentTimeMillis()-t1));
        return Result.successResult(resGoodsInfoVo);
    }

    @GetMapping(value = "/get/goodsInfoByNameOrCode/{store}/{nameOrCode}")
    public Result getGoodsInfoByName(@PathVariable int store,@PathVariable String nameOrCode){
        return Result.successResult(goodsService.getGoodsInfoByNameOrCode(store,nameOrCode));
    }

    @GetMapping(value = "/get/goodsInfo/{store}")
    public Result getGoodsInfoList(@PathVariable int store){
//        long t1=System.currentTimeMillis();
        List<ResGoodsInfoVo> resGoodsInfoVoList=goodsService.getGoodsInfoList(store);
//        log.info(String.valueOf(System.currentTimeMillis()-t1));
        return Result.successResult(resGoodsInfoVoList);
    }
    @GetMapping(value = "/get/goodsInfo2/{store}")
    public Result getGoodsInfoList2(@PathVariable int store){
//        long t1=System.currentTimeMillis();
        List<ResGoodsInfoVo> resGoodsInfoVoList=goodsService.getGoodsInfoList2(store);
//        log.info(String.valueOf(System.currentTimeMillis()-t1));
        return Result.successResult(resGoodsInfoVoList);
    }
    @GetMapping(value = "/get/goodsCodeList/{store}")
    public Result getGoodsCodeList(@PathVariable int store){
        return Result.successResult(goodsService.getGoodsCodeList(store));
    }
    @GetMapping(value = "/get/goodsInfoByType/{type}/{store}")
    public Result getGoodsInfoListByType(@PathVariable String type,@PathVariable int store){
        return Result.successResult(goodsService.getGoodsInfoListByType(type,store));
    }

    @GetMapping(value = "/get/suggestedPrice/{code}")
    public Result getSuggestedPrice(@PathVariable String code){
        return Result.successResult(goodsService.getSuggestedPrice(code));
    }

    @AddLog(type = "test")
    @PostMapping(value = "/test")
    public Result test(@RequestBody GoodsStore goodsStore){
        return Result.successResult(goodsStore);
    }

    @AddLog(type = "新建商品入库")
    @PostMapping(value = "/add/goodsStore")
    public Result addGoodsByStore(@RequestBody List<GoodsStore> goodsStoreList){
        goodsService.addGoodsStore(goodsStoreList);
        return Result.successResult();
    }

    @PostMapping (value = "/importExcel")//测
    public Result importExcel(@RequestBody ImportExcelBo importExcelBo){
        goodsService.importExcel(importExcelBo);
        return Result.successResult();

    }

    @GetMapping(value = "/get/typeList")
    public Result getGoodsTypeList(){
        return Result.successResult(goodsService.getGoodsTypeList());
    }

    @PostMapping(value = "/del/goodsStore")
    public Result delGoodsStoreList(@RequestBody DelGoodsStoreBo delGoodsStoreBo){
        goodsService.delGoodsStoreList(delGoodsStoreBo);
        return Result.successResult();
    }

    @PostMapping(value = "/update/goodsStore")
    public Result updateGoodsStore(@RequestBody List<GoodsStore> goodsStoreList){
        goodsService.updateGoodsStore(goodsStoreList);
        return Result.successResult();
    }

    @AddLog(type = "入库")
    @PostMapping(value = "/update/addGoodsNum")//测
    public Result addGoodsNum(@RequestBody List<AddOrOutGoodsNumBo> addOrOutGoodsNumBoList){
        goodsService.addGoodsNum(addOrOutGoodsNumBoList);
        return Result.successResult();
    }
    @AddLog(type = "出库")
    @PostMapping(value = "/update/outGoodsNum")//测
    public Result outGoodsNum(@RequestBody List<AddOrOutGoodsNumBo> addOrOutGoodsNumBoList){
        goodsService.outGoodsNum(addOrOutGoodsNumBoList);
        return Result.successResult();
    }


    @GetMapping(value = "/get/newGoodsStore/{store}")
    public Result getNewGoodsStore(@PathVariable int store){
//        long t1=System.currentTimeMillis();
        List<ResGoodsSalesAnalysisVo> resGoodsSalesAnalysisVoList=goodsService.getNewGoodsStore(store);
//        log.info(String.valueOf(System.currentTimeMillis()-t1));
        return Result.successResult(resGoodsSalesAnalysisVoList);
    }

    @GetMapping(value = "/get/goodsHotSale/{store}")
    public Result getGoodsHotSale(@PathVariable int store){
//        long t1=System.currentTimeMillis();
        List<ResGoodsSalesAnalysisVo> resGoodsSalesAnalysisVoList=goodsService.getGoodsHotSale(store);
//        log.info(String.valueOf(System.currentTimeMillis()-t1));
        return Result.successResult(resGoodsSalesAnalysisVoList);
    }

    @GetMapping(value = "/get/goodsUnsalable/{store}")
    public Result getGoodsUnsalable(@PathVariable int store){
        return Result.successResult(goodsService.getGoodsUnsalable(store));
    }

    @GetMapping(value = "/get/frequentPurchase/{store}")
    public Result getFrequentPurchase(@PathVariable int store){
        return Result.successResult(goodsService.getFrequentPurchase(store));
    }

    @PostMapping(value = "/add/goodsPurchase")
    public Result addGoodsPurchase(@RequestBody GoodsPurchase goodsPurchase){
        return Result.successResult(goodsService.addGoodsPurchase(goodsPurchase));
    }

    @PostMapping(value = "/del/goodsPurchase")
    public Result delGoodsPurchase(@RequestBody List<Integer> ids){
        return Result.successResult(goodsService.delGoodsPurchase(ids));
    }

    @PostMapping(value = "/update/goodsPurchase")
    public Result updateGoodsPurchase(@RequestBody GoodsPurchase goodsPurchase){
        return Result.successResult(goodsService.updateGoodsPurchase(goodsPurchase));
    }
    @GetMapping(value = "/get/goodsPurchase/{store}")
    public Result getGoodsPurchase(@PathVariable int store, @RequestParam int isDone){
        return Result.successResult(goodsService.getGoodsPurchase(isDone,store));
    }

    @AddLog(type = "一键入库")
    @PostMapping(value = "/update/oneKeyAddGoodsNum")//测
    public Result oneKeyAddGoodsNum(@RequestBody GoodsPurchase goodsPurchase){
        return Result.successResult(goodsService.oneKeyAddGoodsNum(goodsPurchase));
    }

    @PostMapping(value = "/get/GoodsLogByPeriod")
    public Result getGoodsLogByPeriod(@RequestBody GetGoodsLogByPeriodBo getGoodsLogByPeriodBo){
        return Result.successResult(logService.getGoodsLogByPeriod(getGoodsLogByPeriodBo));
    }
}
